var timeId = null;
var timerRunning = false;

function startClock(obj) {
	stopClock();
	showTime(obj);
}

function stopClock() {
	if (timerRunning) {
		clearTimeout(timerID);
		timerRunning = false;
	}
}

function showTime(objId) {
	var now = new Date();
	var year = now.getFullYear();
	var week = now.getDay();
	var month = now.getMonth() + 1;
	var day = now.getDate();
	var hours = now.getHours();
	var minutes = now.getMinutes();
	var seconds = now.getSeconds();
	var timeValue = "" + year;
	timeValue += ((month < 10) ? ":0" : ":") + month;
	timeValue += ((day < 10) ? ":0" : ":") + day;
	timeValue += getWeekStr(week);
	timeValue += ((hours < 10) ? " 0" : " ") + hours;
	timeValue += ((minutes < 10) ? ":0" : ":") + minutes;
	timeValue += ((seconds < 10) ? ":0" : ":") + seconds;
	document.getElementById(objId).innerHTML = timeValue;
	timerID = setTimeout("showTime('"+objId+"')", 1000);
	timerRunning = true;
}

function showMessage(messageType, message, confirm) {
	var messageArea = document.createElement("div");
	messageArea.className = "message-area";

	var container = document.createElement("div");
	container.className = "container";
	
	var title = document.createElement("div");
	title.className = "title";
	
	if (messageType == "W") {
		title.className += " warning";
	} else if (messageType == "E") {
		title.className += " error";
	} else {
		title.className += " info";
	}
	
	var body = document.createElement("div");
	body.className = "body";
	body.innerText = message;
	console.log(message);
	
	var footer = document.createElement("div");
	footer.className = "footer";
	
	if (confirm !== undefined && typeof confirm == 'function') {
		var confirmDiv = document.createElement("div");
		confirmDiv.className = "button-put button-item font-normal font-white";
		confirmDiv.innerText = "确定";
		confirmDiv.addEventListener("click", function() {
			confirm();
			document.body.removeChild(messageArea);
		});
		footer.appendChild(confirmDiv);
	}
	
	var cancel = document.createElement("div");
	cancel.className = "button-del button-item font-normal font-white";
	cancel.innerText = "关闭";
	cancel.addEventListener("click", function() {
		document.body.removeChild(messageArea);
	});
	footer.appendChild(cancel);
	
	messageArea.appendChild(container);
	container.appendChild(title);
	container.appendChild(body);
	container.appendChild(footer);
	
	document.body.appendChild(messageArea);
}

function getWeekStr(week) {
	var result = "";
	switch (week) {
		case 0: 
			result = "星期日";
			break;
		case 1: 
			result = "星期一";
			break;
		case 2: 
			result = "星期二";
			break;
		case 3: 
			result = "星期三";
			break;
		case 4: 
			result = "星期四";
			break;
		case 5: 
			result = "星期五";
			break;
		case 6: 
			result = "星期六";
			break;
	}
	return "（"+result+"）";
}

function jumpToMenuContent(url) {
	window.parent.frames["content"].location.href = "../../"+url+".html";
}

function pxToNum(pxStr) {
	return parseFloat(pxStr.replace("px", "").replace("PX", ""));
}

function formatyyyyMMdd(pDate) {
	var year = pDate.getFullYear();
	var week = pDate.getDay();
	var month = pDate.getMonth() + 1;
	var day = pDate.getDate();
	var timeValue = "" + year;
	timeValue += ((month < 10) ? "-0" : "-") + month;
	timeValue += ((day < 10) ? "-0" : "-") + day;
	
	return timeValue;
}

function getCalendarObject(startDate, days) {

	var result = {};
	
	var tempMsec = +startDate;
	
	result[formatyyyyMMdd(startDate)] = {};
	result[formatyyyyMMdd(startDate)].week = startDate.getDay();
	result[formatyyyyMMdd(startDate)].weekStr = getWeekStr(startDate.getDay());
	
	for (var index = 1; index < days; index++) {
		var nextDate = new Date(tempMsec + 1000*60*60*24*index);
		result[formatyyyyMMdd(nextDate)] = {};
		result[formatyyyyMMdd(nextDate)].week = nextDate.getDay();
		result[formatyyyyMMdd(nextDate)].weekStr = getWeekStr(nextDate.getDay());
	}
	
	return result;
}
//刷新验证码
function refresh(obj) {
    obj.src = "ImageServlet?"+Math.random();
}

